var user_role=sessionStorage.getItem('user_role')
var user_id=sessionStorage.getItem('user_id')
var user_name=sessionStorage.getItem('user_name')
let add_task_div = document.getElementById('add_task');

taskSubmit=function (){
    var id = $("#toWho").val();
    var name = $("#toWhoName").val();
    var content = $("#content").val();
    var dueTime = $("#dueTime").val();
    var workload = $("#workload").val();
    if(content==="" || workload === "" || dueTime === ""){
        alert("请完善任务信息！");
        return;
    }
    var myDate = new Date();
    var startTime = myDate.toLocaleDateString();
    startTime = startTime.replace(/\//g,"-");
    data={}
    data.leaderID = user_id;
    data.leaderName = user_name;
    data.toWho = id;
    data.toWhoName = name;
    data.content = content;
    data.workload = workload;
    data.status = "todo";
    data.progress = 0;
    data.startTime = startTime;
    data.dueTime = dueTime;
    data.action = "add_task";
    console.log(data);
    $.post("../project/action1", data, function (json) {
        json = JSON.parse(json);
        if (json.result === 1) {
            alert("布置成功！请到<进度管理> -> <进度查询>中查看任务信息");
        }
        if (json.result === 0) {
            alert("布置失败");
        }
        document.location.reload();
    })
}

// 布置任务
addTask=function (member_id, member_name){
    console.log(member_id+"  "+member_name);
    $("#toWho").val(member_id);
    $("#toWhoName").val(member_name);
    $('#add_task').modal('show');
   //document.getElementById('add_submit_button').addEventListener('click', taskSubmit());//任务布置监听器
}
    // 显示用户信息
showDataTable=function (){
$('.datatable').dataTable({
    autoWidth: true,
    paging: true,
    searching: true,
    aaSorting: [], //不让其按第一列排序
    oLanguage: {
        aria: {
            sortAscending: ': activate to sort column ascending',
            sortDescending: ': activate to sort column descending',
        },
        sProcessing: '处理中...',
        sLengthMenu: '_MENU_ 记录/页',
        sZeroRecords: '没有匹配的记录',
        sInfo: '显示第 _START_ 至 _END_ 项记录，共 _TOTAL_ 项',
        sInfoEmpty: '显示第 0 至 0 项记录，共 0 项',
        sInfoFiltered: '(由 _MAX_ 项记录过滤)',
        sInfoPostFix: '',
        sSearch: '请输入查询信息:',
        oPaginate: {
            sFirst: '首页',
            sPrevious: '上页',
            sNext: '下页',
            sLast: '末页',
        },
    },
    // "aoColumnDefs": [{"bSearchable": false,"bVisible":false,"aTargets":[0]}],
    aoColumns: [
        {
            mRender: function (data, type, full) {
                sReturn = full.user_name
                return sReturn
            },
            orderable: false,
        },
        {
            mRender: function (data, type, full) {
                sReturn = full.user_email
                return sReturn
            },
            orderable: false,
        },
        {
            mRender: function (data, type, full) {
                sReturn = full.user_mobile
                return sReturn
            },
            orderable: false,
        },
        {
            mRender: function (data, type, full) {
                sReturn = full.user_role
                return sReturn
            },
            orderable: true,
        },
        {
            mRender: function (data, type, full) {
                sReturn ="<button class='btn btn-xs btn-primary' onclick=\"addTask("+full.id+",'"+full.user_name+"')\">布置任务</button>";
                return sReturn
            },
            orderable: false,
        },
    ],
    aLengthMenu: [
        [5, 10, 15, 20, 25, 40, 50, -1],
        [5, 10, 15, 20, 25, 40, 50, '所有记录'],
    ],
    fnDrawCallback: function () {
        $('.checkboxes').uniform()
        $('.groupDao.java-checkable').uniform()
    },

    ajax: {
        async: false,
        url: '../userFile',
        type: 'POST',
        data: { user_role: this.user_role, user_id: this.user_id },
        dataSrc: function (result) {
            return result.user_list
        },
    },
})
$('.datatable')
    .find('.groupDao.java-checkable')
    .change(function () {
        var set = jQuery(this).attr('data-set')
        var checked = jQuery(this).is(':checked')
        jQuery(set).each(function () {
            if (checked) {
                $(this).attr('checked', true)
                $(this).parents('tr').addClass('active')
            } else {
                $(this).attr('checked', false)
                $(this).parents('tr').removeClass('active')
            }
        })
        jQuery.uniform.update(set)
    })
$('.datatable').on('change', 'tbody tr .checkboxes', function () {
    $(this).parents('tr').toggleClass('active')
})
}

if(user_role==="组长"){
    add_task_div.style.display='none';
    showDataTable();
}
